<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Into extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		set_time_limit(0);
		$coin_id = intval($this->input->get('cid'));

		if ($coin_id) {
			$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $coin_id . ' and status=1'));
		}
		else {
			$coin = $this->d->find(array('table' => 'coin', 'where' => 'status=1', 'order' => 'sort asc'));
		}

		$xnb = $this->d->find(array('table' => 'xnb', 'where' => 'user_id=' . $this->user['id'] . ' and coin_id=' . $coin['id']));
		$address = $xnb['address'];

		if (!$address) {
			if ((strtolower($coin['en_name']) != 'eth') && $coin['host'] && $coin['username'] && $coin['password'] && $coin['port']) {
				$address = $this->get_address($coin['host'], $coin['username'], $coin['password'], $coin['port'], $this->user['username']);
			}
			else {
				if ((strtolower($coin['en_name']) == 'eth') && $coin['host'] && $coin['port']) {
					if (($this->cfg['is_eth_fill'] == 1) && $this->cfg['main_account_pwd']) {
						$address = $this->get_ethaddress($coin['host'], $coin['port'], $this->cfg['main_account_pwd']);
					}
				}
				else {
					$address = '';
				}
			}

			if ($address) {
				if (!$this->db->where('id', $xnb['id'])->set('address', $address)->update('xnb')) {
					$address = '';
				}
			}
		}

		$where['coin_id'] = $coin['id'];
		$where['user_id'] = $this->user['id'];
		$d = $this->result_list(array('table' => 'fill_coin', 'where' => $where, 'url' => 'into/index', 'order' => 'add_time desc', 'per_page' => 10));
		$d['coin'] = $coin;
		$d['cfg'] = $this->cfg;
		$d['address'] = $address;
		$d['coins'] = $this->d->rows(array('table' => 'coin', 'where' => 'status=1', 'order' => 'sort asc'));
		$this->show(array('header', 'user_left', 'coin_fill', 'footer'), $d);
	}

	public function cancel()
	{
		$id = intval($this->input->post('id'));

		if ($id <= 0) {
			ajax_return(1, '找不到订单');
		}

		$fill = $this->db->where('id', $id)->where('status', 0)->where('user_id', $this->session->user_id)->get('fill_coin')->row_array();

		if (!$fill) {
			ajax_return(1, '找不到可撤销的订单');
		}

		$rs = $this->db->where('id', $id)->update('fill_coin', array('status' => 3));

		if ($rs) {
			ajax_return(0, '撤销成功');
		}
		else {
			ajax_return(1, '撤销失败');
		}
	}
}

?>
